﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package GameMode {
    import flash.events.*;
    import Engine.Managers.*;
    import Engine.Interfaces.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Events.*;
    import flash.utils.*;
    import Classes.*;
    import Display.*;
    import Engine.*;

    public class GameMode implements IInputHandler {

        protected static var s_gotVScrollCap:Boolean = false;
        protected static var s_useVScrollCap:Boolean = false;
        protected static var s_VScrollCap:Number = 0;

        protected var m_enabled:Boolean = false;
        protected var m_cursorImage:Object = null;
        protected var m_cursorId:Number = 0;
        protected var m_mouseDownPos:Point;
        protected var m_captureMouse:Boolean = false;
        protected var m_showMousePointer:Boolean = false;
        protected var m_scrollTimer:Timer;
        protected var m_displayObject:Sprite;
        protected var m_selectedLotSite:LotSite = null;
        public var m_spotlightOn:Boolean = false;
        protected var m_uiMode:String;

        public function GameMode():void{
            this.m_scrollTimer = new Timer(125);
            this.m_displayObject = new Sprite();
            super();
            this.m_uiMode = UIEvent.OTHER;
        }
        protected static function undoZoomTransformations(_arg1:Number, _arg2:Number):Number{
            _arg1 = (_arg1 - (_arg2 / 2));
            _arg1 = (_arg1 * GlobalEngine.viewport.getZoom());
            _arg1 = (_arg1 + (_arg2 / 2));
            return (_arg1);
        }
        protected static function redoZoomTransformations(_arg1:Number, _arg2:Number):Number{
            _arg1 = (_arg1 - (_arg2 / 2));
            _arg1 = (_arg1 / GlobalEngine.viewport.getZoom());
            _arg1 = (_arg1 + (_arg2 / 2));
            return (_arg1);
        }
        public static function limitBackgroundScrolling(_arg1:Vector2):Vector2{
            var _local7:Number;
            var _local9:Number;
            var _local11:Number;
            var _local13:Number;
            var _local15:XML;
            var _local16:XMLList;
            var _local17:String;
            var _local18:Number;
            var _local19:Number;
            var _local20:Number;
            if (Global.world.overlayBackgroundRect == null){
                return (GlobalEngine.viewport.getScrollPosition());
            };
            var _local2:Number = (Global.world.overlayBackgroundRect.width * GlobalEngine.viewport.getZoom());
            var _local3:Number = (Global.world.overlayBackgroundRect.height * GlobalEngine.viewport.getZoom());
            while ((((_local2 < GlobalEngine.viewport.stage.stageWidth)) && ((GlobalEngine.viewport.getZoom() < Global.gameSettings.getNumber("maxZoom"))))) {
                GlobalEngine.viewport.setZoom((Global.gameSettings.getNumber("zoomStep") + GlobalEngine.viewport.getZoom()));
                _local2 = (Global.world.overlayBackgroundRect.width * GlobalEngine.viewport.getZoom());
                _local3 = (Global.world.overlayBackgroundRect.height * GlobalEngine.viewport.getZoom());
            };
            if (!s_gotVScrollCap){
                s_gotVScrollCap = true;
                _local15 = Global.gameSettings.getXML();
                _local16 = _local15.collisionBackground.scrollLock;
                if (_local16 != null){
                    _local17 = _local16.@verticalTiles;
                    if (_local17 == null){
                        s_useVScrollCap = false;
                        s_VScrollCap = 0;
                    } else {
                        s_useVScrollCap = true;
                        s_VScrollCap = int(_local17);
                    };
                };
            };
            if (s_useVScrollCap){
                _local18 = IsoMath.getPixelDeltaFromTileDelta(1, 1).y;
                _local19 = s_VScrollCap;
                _local20 = ((-(_local18) * _local19) * GlobalEngine.viewport.getZoom());
                if (_local20 < (_local3 / 2)){
                    _local3 = (_local3 - ((_local3 / 2) - _local20));
                };
            };
            var _local4:Point = new Point();
            _local4.x = _arg1.x;
            _local4.y = _arg1.y;
            var _local5:Number = Global.world.overlayBackgroundRect.x;
            var _local6:Number = Global.world.overlayBackgroundRect.y;
            _local4.x = (_local4.x + _local5);
            _local4.y = (_local4.y + _local6);
            _local4.x = undoZoomTransformations(_local4.x, GlobalEngine.viewport.stage.stageWidth);
            _local4.y = undoZoomTransformations(_local4.y, GlobalEngine.viewport.stage.stageHeight);
            var _local8:Number = 0;
            _local7 = redoZoomTransformations(_local8, GlobalEngine.viewport.stage.stageWidth);
            _local7 = (_local7 - _local5);
            var _local10:Number = (GlobalEngine.viewport.stage.stageWidth - _local2);
            _local9 = redoZoomTransformations(_local10, GlobalEngine.viewport.stage.stageWidth);
            _local9 = (_local9 - _local5);
            var _local12:Number = 0;
            _local11 = redoZoomTransformations(_local12, GlobalEngine.viewport.stage.stageHeight);
            _local11 = (_local11 - _local6);
            var _local14:Number = (GlobalEngine.viewport.stage.stageHeight - _local3);
            _local13 = redoZoomTransformations(_local14, GlobalEngine.viewport.stage.stageHeight);
            _local13 = (_local13 - _local6);
            if (_local2 >= GlobalEngine.viewport.stage.stageWidth){
                if (_local4.x > 0){
                    _arg1.x = _local7;
                };
                if ((_local4.x + _local2) < GlobalEngine.viewport.stage.stageWidth){
                    _arg1.x = _local9;
                };
            } else {
                if (_local4.x < 0){
                    _arg1.x = _local7;
                };
                if ((_local4.x + _local2) > GlobalEngine.viewport.stage.stageWidth){
                    _arg1.x = _local9;
                };
            };
            if (_local3 >= GlobalEngine.viewport.stage.stageHeight){
                if (_local4.y > 0){
                    _arg1.y = _local11;
                };
                if ((_local4.y + _local3) < GlobalEngine.viewport.stage.stageHeight){
                    _arg1.y = _local13;
                };
            } else {
                if (_local4.y < 0){
                    _arg1.y = _local11;
                };
                if ((_local4.y + _local3) > GlobalEngine.viewport.stage.stageHeight){
                    _arg1.y = _local13;
                };
            };
            _arg1.y = Math.max(_arg1.y, -25);
            return (_arg1);
        }

        public function get supportsEditing():Boolean{
            return (true);
        }
        public function getToolboxId():String{
            return (null);
        }
        public function get uiMode():String{
            return (this.m_uiMode);
        }
        public function enableMode():void{
            InputManager.addHandler(this);
            if (this.m_captureMouse){
                Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp, false, 1, true);
                Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove, false, 1, true);
            };
            if (this.m_cursorImage){
                this.m_cursorId = UI.setCursor(this.m_cursorImage, this.m_showMousePointer);
            };
            if (Global.ui){
                Global.ui.updateToolIcon(this);
            };
            GlobalEngine.viewport.overlayBase.addChild(this.m_displayObject);
            if (Config.VIEWPORT_CLEAR_COLOR != this.viewportClearColor){
                Config.VIEWPORT_CLEAR_COLOR = this.viewportClearColor;
            };
            this.m_enabled = true;
        }
        public function disableMode():void{
            this.setViewScrollEnabled(false);
            this.m_enabled = false;
            InputManager.removeHandler(this);
            if (Global.stage != null){
                Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
                Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            };
            if (this.m_cursorId != 0){
                UI.removeCursor(this.m_cursorId);
                this.m_cursorId = 0;
            };
            if (((this.m_displayObject) && (this.m_displayObject.parent))){
                this.m_displayObject.parent.removeChild(this.m_displayObject);
            };
        }
        public function removeMode():void{
        }
        protected function get viewportClearColor():uint{
            return (EmbeddedArt.VIEWPORT_CLEAR_COLOR);
        }
        protected function setViewScrollEnabled(_arg1:Boolean):void{
            this.m_scrollTimer.removeEventListener(TimerEvent.TIMER, this.onScrollTimer);
            this.m_scrollTimer.stop();
            if (_arg1){
                this.m_scrollTimer.addEventListener(TimerEvent.TIMER, this.onScrollTimer);
                this.m_scrollTimer.start();
            };
        }
        protected function getMouseStagePos():Point{
            return (new Point(Global.stage.mouseX, Global.stage.mouseY));
        }
        protected function getMouseTilePos(_arg1:MouseEvent=null, _arg2:Point=null):Point{
            var _local3:Point;
            if (_arg1){
                _local3 = new Point(_arg1.stageX, _arg1.stageY);
            } else {
                _local3 = this.getMouseStagePos();
            };
            if (_arg2){
                _local3 = _local3.subtract(_arg2);
            };
            return (IsoMath.screenPosToTilePos(_local3.x, _local3.y));
        }
        protected function onScrollTimer(_arg1:TimerEvent):void{
        }
        public function onMouseOut(_arg1:MouseEvent):Boolean{
            return (false);
        }
        public function onMouseDown(_arg1:MouseEvent):Boolean{
            this.m_mouseDownPos = new Point(_arg1.stageX, _arg1.stageY);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp, false, 1, true);
            Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove, false, 1, true);
            return (false);
        }
        public function onMouseUp(_arg1:MouseEvent):Boolean{
            this.m_mouseDownPos = null;
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
            Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            return (false);
        }
        public function onMouseDoubleClick(_arg1:MouseEvent):Boolean{
            return (false);
        }
        public function onMouseMove(_arg1:MouseEvent):Boolean{
            return (false);
        }
        public function onMouseWheel(_arg1:MouseEvent):Boolean{
            return (false);
        }
        public function onKeyDown(_arg1:KeyboardEvent):Boolean{
            return (false);
        }
        public function onKeyUp(_arg1:KeyboardEvent):Boolean{
            return (false);
        }
        public function isDragging():Boolean{
            return (!((this.m_mouseDownPos == null)));
        }
        public function drawTileArea(_arg1:Graphics, _arg2:Point, _arg3:Number, _arg4:Number):void{
            var _local5:Point = IsoMath.tilePosToPixelPos(_arg2.x, _arg2.y);
            var _local6:Vector2 = IsoMath.getPixelDeltaFromTileDelta(_arg3, 0);
            var _local7:Vector2 = IsoMath.getPixelDeltaFromTileDelta(0, _arg4);
            _arg1.moveTo(_local5.x, _local5.y);
            _arg1.lineTo((_local5.x + _local6.x), (_local5.y + _local6.y));
            _arg1.lineTo(((_local5.x + _local6.x) + _local7.x), ((_local5.y + _local6.y) + _local7.y));
            _arg1.lineTo((_local5.x + _local7.x), (_local5.y + _local7.y));
            _arg1.lineTo(_local5.x, _local5.y);
        }
        public function getLotSite():LotSite{
            return (this.m_selectedLotSite);
        }

    }
}//package GameMode 
